package com.contacts.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.contacts.dao.UserDao;
import com.contacts.model.Contact;
import com.contacts.model.TelBook;
import com.contacts.model.User;
import com.contacts.utils.JDBCUtil;

public class UserDaoImpl implements UserDao{

	@SuppressWarnings("finally")
	@Override
	public User findByNameAndPwd(String uname, String upwd) {
		Connection conn =null;
		PreparedStatement pstat=null;
		ResultSet rs=null;
		User u=null;
		try {
			conn= JDBCUtil.getConnection();
			pstat=conn.prepareStatement("select * from user where uname=? and upwd=?");
			pstat.setString(1, uname);
			pstat.setString(2, upwd);
			rs=pstat.executeQuery();
			if(rs.next()){
				u=new User();
				u.setUid(rs.getInt("uid"));
				u.setUpwd(rs.getString("upwd"));
				u.setUname(rs.getString("uname"));
				u.setUtel(rs.getString("utel"));
			}
			
		} catch (Exception e) {
		}
		finally {
			JDBCUtil.close(conn, pstat, rs);
			return u;
		}
		
	}
    @SuppressWarnings("finally")
	@Override
	public List<TelBook> showTelBook(int uid) {
    	Connection conn =null;
		PreparedStatement pstat=null;
		ResultSet rs=null;
		TelBook tbook=null;
		List<TelBook> list = new ArrayList<>();
		try {
			conn= JDBCUtil.getConnection();
			pstat=conn.prepareStatement("select * from telbook where uid=?");
			pstat.setInt(1, uid);
			rs=pstat.executeQuery();
			if(rs.next()){
				tbook=new TelBook();
				tbook.setTid(rs.getInt("tid"));
				tbook.setTname(rs.getString("tname"));
				tbook.setUid(rs.getInt("uid"));
				list.add(tbook);
			}
			
		} catch (Exception e) {
		}
		finally {
			JDBCUtil.close(conn, pstat, rs);
			
			return list;
		}
    }
    @SuppressWarnings("finally")
	@Override
	public List<Contact> queryContact(int uid, int tid) {
    	Connection conn =null;
		PreparedStatement pstat=null;
		ResultSet rs=null;
		Contact tac=null;
		List<Contact> list = new ArrayList<>();
		try {
			conn= JDBCUtil.getConnection();
			pstat=conn.prepareStatement("select * from contact where uid=? and tid=?;");
			pstat.setInt(1, uid);
			pstat.setInt(2, tid);
			rs=pstat.executeQuery();
			if(rs.next()){
				tac=new Contact();
				tac.setCid(rs.getInt("cid"));
				tac.setCname(rs.getString("cname"));
				tac.setCtel(rs.getString("ctel"));
				tac.setUid(rs.getInt("uid"));
				tac.setTid(rs.getInt("tid"));
				list.add(tac);
			}
			
		} catch (Exception e) {
		}
		finally {
			JDBCUtil.close(conn, pstat, rs);
			
		return list;
	}	
	
    }
    @Override
	public boolean addContact(String cname, String ctel, int uid, int tid) throws SQLException {
    	Connection conn = JDBCUtil.getConnection();
		PreparedStatement ps = null;
		boolean flag = false;
		String sql = "insert into contact values(null,?,?,?,?);";
		ps = conn.prepareStatement(sql);
		ps.setString(1, cname);
		ps.setString(2, ctel);
		ps.setInt(3, uid);
		ps.setInt(4, tid);
		int state = ps.executeUpdate();
		if(state == 1){
			flag = true;
		}
		return flag;
	}
    @Override
	public boolean deleteContact(int cid) throws SQLException {
    	Connection conn = JDBCUtil.getConnection();
		PreparedStatement ps = null;
		boolean flag = false;
		String sql = "delete from contact where cid=?;";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, cid);
		int state = ps.executeUpdate();
		if(state == 1){
			flag = true;
		}
		return flag;
	}
    @Override
	public boolean changeContact(String newName, String newTel, int cid) throws SQLException {
    	Connection conn = JDBCUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update contact set cname=?,ctel=?where cid=?;";
		ps = conn.prepareStatement(sql);
		ps.setString(1, newName);
		ps.setString(2, newTel);
		ps.setInt(3, cid);
		int state = ps.executeUpdate();
		Boolean flag = false;
		if(state == 1){
		   flag = true;
		}
		JDBCUtil.close(conn, ps);
		return flag;
	}
    @SuppressWarnings("finally")
	@Override
	public Contact serchContact(String cname,int uid) {
    	Connection conn =null;
		PreparedStatement pstat=null;
		ResultSet rs=null;
		Contact tac=null;
		try {
			conn= JDBCUtil.getConnection();
			pstat=conn.prepareStatement("select * from contact where cname like ?and uid=?;");
			pstat.setString(1, cname);
			pstat.setInt(2, uid);
			rs=pstat.executeQuery();
			if(rs.next()){
				tac=new Contact();
				tac.setCid(rs.getInt("cid"));
				tac.setCname(rs.getString("cname"));
				tac.setCtel(rs.getString("ctel"));
				tac.setUid(rs.getInt("uid"));
				tac.setTid(rs.getInt("tid"));
			}
			
		} catch (Exception e) {
		}
		finally {
			JDBCUtil.close(conn, pstat, rs);
			return tac;
	}
}
}
